import pandas as pd
import datetime


# 通过一个序列来生成一个31*(count(*)-train_end)矩阵（用于处理时序的数据）
# 其中最后一列维标签数据。就是把当天的前n天作为参数，当天的数据作为label
def generate_data_by_n_days(series, n, index):
    if len(series) <= n:
        raise Exception("The Length of series is %d, while affect by (n=%d)." % (len(series), n))
    df = pd.DataFrame()
    for i in range(n):
        df['c%d' % i] = series.tolist()[i:-(n - i)]
    df['y'] = series.tolist()[n:]

    if index:
        df.index = series.index[n:]
    return df



# 参数n与上相同。train_end表示的是后面多少个数据作为测试集。
def readData(path,column, n , all_too, index,train_end ):

    df = pd.read_csv(path,index_col='date')
   # df = df.drop(columns=['id'])

    # 以日期为索引
    df.index = list(map(lambda x: datetime.datetime.strptime(x, "%Y-%m-%d"), df.index))
    # 获取每天的最高价
    df_column = df[column].copy()
    # 拆分为训练集和测试集
    # [:-500]  2358-500= 1858 训练集数据df_column_train 0~1858     [-530:]df_column_test 1828~2358
    df_column_train, df_column_test = df_column[:train_end], df_column[train_end - n:]
    print(len(df_column_train))
    print(len(df_column_test))

    # 生成训练数据
    df_generate_train = generate_data_by_n_days(df_column_train, n, index)
    if all_too:
        return df_generate_train, df_column, df.index.tolist()
    return df_generate_train
